<?php
use \GatewayWorker\Lib\Gateway;
class PM_x
{

    /**
     * 实时语音聊天
     * @param $room_id
     * @param $user_id
     * @param $exp_id
     * @param $db_connect
     */
    public static function __Chat_Wave( $room_id , $user_id , $serverID , $db_connect ){
        if( $room_id == "" ){return false;}
        //取出房间信息
        $room_info  = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        if( $room_info == false || $room_info == null || count( $room_info ) <= 0 ){return false;}
        //加入BUG修复：如果房间不是正在运行的状态，是不能发聊天消息的
        if( $room_info['room_status'] != 1 ){ return false; }
        //向房间里每个在线的用户发送语音信息
        foreach( $room_info as $key => $value ) {
            if (strstr($key, 'room_player_') != false && $value != 0 ) {
                $user_clientID = PHZ_Member::__getUserClientID( $value , $db_connect );
                if( $user_clientID != "" && Gateway::isOnline( $user_clientID ) == 1 ){
                    $data = array();
                    $data['log_uid']     = (int)$user_id;//发送者的用户ID
                    $data['server_id']   = $serverID;
                    $data['log_to_uid']  = $value;
                    $data['log_md']      = 26;//微信语音聊天
                    $data['log_data']    = json_encode( array() );
                    Gateway::sendToClient( $user_clientID , json_encode( $data ) ) ;
                    /***************************************************  BUG:发语音的人有时候会显示离线状态 **************/
                    $data = array();
                    $data['log_uid']     = (int)$user_id;
                    $data['log_md']      = 28;
                    Gateway::sendToClient( $user_clientID , json_encode( $data ) ) ;
                    /***************************************************  BUG:发语音的人有时候会显示离线状态 **************/
                }
            }
        }
    }

    /**
     * 表情聊天
     * @param $room_id    房间号
     * @param $user_id    发送用户
     * @param $exp_id     表情ID
     * @param $db_connect 数据链接
     */
    public static function __Chat_Expression( $room_id , $user_id , $exp_id , $db_connect ){
        if( $room_id == "" ){return false;}
        //取出房间信息
        $room_info  = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        if( $room_info == false || $room_info == null || count( $room_info ) <= 0 ){return false;}
        //加入BUG修复：如果房间不是正在运行的状态，是不能发聊天消息的
        if( $room_info['room_status'] != 1 ){ return false; }
        //向房间里每个在线的用户发送表情信息
        foreach( $room_info as $key => $value ) {
            if (strstr($key, 'room_player_') != false && $value != 0 ) {
                $user_clientID = PHZ_Member::__getUserClientID( $value , $db_connect );
                if( $user_clientID != "" && Gateway::isOnline( $user_clientID ) == 1 ){
                    $data = array();
                    $data['log_uid']     = (int)$user_id;//发送者的用户ID
                    $data['exp_id']      = $exp_id;
                    $data['log_to_uid']  = $value;
                    $data['log_md']      = 21;//表情聊天
                    $data['log_data']    = json_encode( array() );
                    Gateway::sendToClient( $user_clientID , json_encode( $data ) ) ;
                }
            }
        }
    }

    /**
     * 文字聊天（带音效） - 其中文字编号跟音效编号一致( 0-6 )
     * @param $room_id
     * @param $user_id
     * @param $text_id
     * @param $db_connect
     */
    public static function __Chat_Text( $room_id , $user_id , $text_id , $db_connect ){
        if( $room_id == "" ){return false;}
        //取出房间信息
        $room_info  = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        if( $room_info == false || $room_info == null || count( $room_info ) <= 0 ){return false;}
        //加入BUG修复：如果房间不是正在运行的状态，是不能发聊天消息的
        if( $room_info['room_status'] != 1 ){ return false; }
        //向房间里每个在线的用户发送表情信息
        foreach( $room_info as $key => $value ) {
            if (strstr($key, 'room_player_') != false && $value != 0 ) {
                $user_clientID = PHZ_Member::__getUserClientID( $value , $db_connect );
                if( $user_clientID != "" && Gateway::isOnline( $user_clientID ) == 1 ){
                    $data = array();
                    $data['log_uid']     = (int)$user_id;//发送者的用户ID
                    $data['text_id']     = $text_id;
                    $data['log_to_uid']  = $value;
                    $data['log_md']      = 22;//音效聊天
                    $data['log_data']    = json_encode( array() );
                    Gateway::sendToClient( $user_clientID , json_encode( $data ) ) ;
                }
            }
        }
    }

    /**
     * 纯文字聊天
     * @param $room_id
     * @param $user_id
     * @param $msg_info
     * @param $db_connect
     * @return bool
     */
    public static function __Chat_Msg( $room_id , $user_id , $msg_info , $db_connect ){
        if( $room_id == "" ){return false;}
        //取出房间信息
        $room_info  = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        if( $room_info == false || $room_info == null || count( $room_info ) <= 0 ){return false;}
        //加入BUG修复：如果房间不是正在运行的状态，是不能发聊天消息的
        if( $room_info['room_status'] != 1 ){ return false; }
        //向房间里每个在线的用户发送表情信息
        foreach( $room_info as $key => $value ) {
            if (strstr($key, 'room_player_') != false && $value != 0 ) {
                $user_clientID = PHZ_Member::__getUserClientID( $value , $db_connect );
                if( $user_clientID != "" && Gateway::isOnline( $user_clientID ) == 1 ){
                    $data = array();
                    $data['log_uid']     = (int)$user_id;//发送者的用户ID
                    $data['msg_info']    = $msg_info;
                    $data['log_to_uid']  = $value;
                    $data['log_md']      = 23;//输入文字聊天
                    $data['log_data']    = json_encode( array() );
                    Gateway::sendToClient( $user_clientID , json_encode( $data ) ) ;
                }
            }
        }
    }

    /** 根据用户提交的经纬度，获取具体的位置详情，并保存到数据库，供后面的相关使用
     * @param $user_id
     * @param $jd
     * @param $wd
     * @param $db_connect
     * @return string
     */
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static function __QueryLocal( $user_id , $jd , $wd , $ip ,  $db_connect ){
        $host    = "http://jisujwddz.market.alicloudapi.com";
        $path    = "/geoconvert/coord2addr";
        $method  = "GET";
        $appcode = "714625f2af8a4db0888374aace1a7857";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "lat=". $jd ."&lng=". $wd ."&type=baidu";
        $bodys  = "";
        $url = $host . $path . "?" . $querys;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $local_info = curl_exec($curl);
        if( $local_info != "" ){
            $local_info = json_decode($local_info,true);
            if( $local_info['status'] == 0 && $local_info['msg'] == 'ok' ){
                $location = $local_info['result']['city'] . $local_info['result']['description'];
                //更新当前用户的地理位置信息(包括精度，纬度，以及具体详情)
                $cols = array();
                $cols['location']  = $location;
                $cols['latitude']  = $local_info['result']['lat'];
                $cols['longitude'] = $local_info['result']['lng'];
                $cols['ip']        = $ip;
                $db_connect->update('tt_member')->cols( $cols )->where('uid=' . $user_id )->query();
                //返回地址信息
                return $location;
            }
        }
        return "";
    }

}